#!/usr/bin/python
#-*- coding: utf-8 -*-

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.arrays import vbo
from numpy import array
from collections import namedtuple
import sys
import math

animate = False
angle = 0
WIDTH, HEIGHT = 500, 500

def initGL(w, h):
    glClearColor(1,1,1,1)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45., float(w)/h, 1., 100.)
    glMatrixMode(GL_MODELVIEW)


def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()

    
    # Teapot 1
    glPushMatrix() # sicher Ausgangszustand
    glTranslate(0.0, 0.8, -6.0)
    glRotate(angle, 0., 1., 0.) # um Y-Achse drehen
    glColor3f(0., 0., 1.)
    glutWireTeapot(1.0)
    glPopMatrix()
    
    # Teapot 2
    glTranslate(0., -1., -6)
    glRotate(-angle, 1.0, 0.0, 0.0)
    glutWireTeapot(1.)

    glutSwapBuffers()
    
def resizeViewport(width, height):
    # Nullteilung verhindern
    if height==0:
        height=1
    # aktuellen Viewport resetten
    glViewport(0, 0, width, height)
    # Reset perspective transformation
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    print 'aspect ratio: {} {}'.format(width, height)
    gluPerspective(45., float(width)/height, 1., 100.)
    glMatrixMode(GL_MODELVIEW)
    glutSwapBuffers()
    

def keyPressed(key, # ASCII Zeichen
               x, y # Mausposition
               ):
    global animate
    if key=='a':        
        animate ^= True
    glutPostRedisplay()

def animation():
    global angle
    if animate: # solange angeschaltet, setze Winkel hoch je aufruf (idleFunc)
        print 'drehee'        
        angle = (angle+1)%360
        glutPostRedisplay()
    
    
def main():
    
    # GLUT initialisieren
    glutInit(sys.argv) # kommandozeilenparameter uebergeben, glut library init
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) # Displaymodus fuer das Window
    glutInitWindowSize(WIDTH,HEIGHT)
    glutCreateWindow("Simple OpenGL Window")

    # Callback-Funktionen registrieren
    glutDisplayFunc(display)

    glutReshapeFunc(resizeViewport)
    glutKeyboardFunc(keyPressed)  # immer klein halten
    glutIdleFunc(animation)
    
   # Hack for Mac OS X
#   cwd = os.getcwd()
#   glutInit(sys.argv)
#   os.chdir(cwd)

    # OpenGL Kontext initialisieren
    initGL(WIDTH, HEIGHT)

    # Start GLUT Mainloop
    glutMainLoop()
    
    
    
if __name__ == '__main__':
    main()
